/**
 * 
 */
var i = 0;
var t = 0;
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function checkProductName(obj) {
	var str = obj.value;
	var s = document.getElementById("product_name");
	// alert(s);
	xmlhttp.open("GET", "ProductServlet?op=checkProductName&product_name="
			+ str, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该名称已经存在</font>";
				checkAddProductName("false");
			}
			else{
				checkAddProductName("true");
			}
		}
	}
	xmlhttp.send();
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>产品名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>产品名称符合规则</font>";
		return true;
	}

}

function checkUpProductName(obj, obj1) {
	var str = obj.value;
	var str1 = obj1.value;
	var s = document.getElementById("check_name");
	// alert(s);
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>产品名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>产品名称符合规则</font>";
		return true;
	}
	xmlhttp.open("GET",
			"../../ProductServlet?op=checkUpProductName&product_name=" + str
					+ "&productID=" + str1, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该名称已经存在</font>";
				change_submit("false");
			} else {
				s.innerHTML = "<font color='green'>产品名称符合规则</font>";
				change_submit("true");
			}

		}
	}
	// if (s.innerHTML.indexOf("该名称已经存在")!=-1);
	// return false;
	xmlhttp.send();
	

}
function checkProductIncome_price(obj) {
	var str = obj.value;
	var s = document.getElementById("income_price");
	if (!isNaN(str) && parseFloat(str) > 0) {
		s.innerHTML = "<font color='green'>进价符合规则</font>";
		return true;
	} else {
		if (!isNaN(str)) {
			s.innerHTML = "<font color='red'>进价必须大于0</font>";
			return false;
		}
		s.innerHTML = "<font color='red'>进价只能输入数字</font>";
		return false;
	}

}
function checkProductQuantity(obj) {
	var str = obj.value;
	var s = document.getElementById("quantity");
	if (parseInt(str) != str || str < 0) {
		s.innerHTML = "<font color='red'>数量只能输入大于0的整数</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>数量符合规则</font>";
		return true;
	}

}
function checkProductSales_price(obj) {
	var str = obj.value;
	var s = document.getElementById("sales_price");
	if (!isNaN(str) && parseFloat(str) > 0) {
		s.innerHTML = "<font color='green'>售价符合规则</font>";
		return true;
	} else {
		if (!isNaN(str)) {
			s.innerHTML = "<font color='red'>售价必须大于0</font>";
			return false;
		}
		s.innerHTML = "<font color='red'>售价只能输入数字</font>";
		return false;
	}

}

function checkCateName(obj) {
	var str = obj.value;
	var s = document.getElementById("category_name");
	xmlhttp.open("GET", "CategoryServlet?op=checkCategoryName&category_name="
			+ str, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该名称已经存在</font>";
			}

		}
	}
	xmlhttp.send();
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>类别名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>账户符合规则</font>";
		return true;
	}
}
function checkUpCateName(obj, obj1) {
	var str = obj.value;
	var str1 = obj1.value;
	var s = document.getElementById("category_name");
	// alert(str1);

	xmlhttp.open("GET", "CategoryServlet?op=checkUpCateName&category_name="
			+ str + "&categoryID=" + str1, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该名称已经存在</font>";
				change_submit("false");
			} else {
				s.innerHTML = "<font color='green'>产品名称符合规则</font>";
				change_submit("true");
			}

		}
	}
	xmlhttp.send();
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>目录名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>目录名称符合规则</font>";
		return true;
	}
}
function checkCateDesc(obj) {
	var str = obj.value;
	var s = document.getElementById("category_desc");
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>类别介绍不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>类别介绍符合规则</font>";
		return true;
	}
}
function CheckProviderName(obj) {
	var str = obj.value;
	var s = document.getElementById("provider_name");

	xmlhttp.open("GET", "ProviderServlet?op=checkProviderName&provider_name="
			+ str, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该供销商已经存在</font>";
			}

		}
	}
	xmlhttp.send();
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>供销商名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>符合规则</font>";
		return true;
	}
}

function CheckUpProviderName(obj, obj1) {
	var str = obj.value;
	var str1 = obj1.value;
	var s = document.getElementById("provider_name");
	xmlhttp.open("GET", "ProviderServlet?op=checkUpProviderName&provider_name="
			+ str + "&providerID=" + str1, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var txt = xmlhttp.responseText;
			if (txt != "true") {
				s.innerHTML = "<font color='red'>该供销商名称已经存在</font>";
				change_submit("false");
			} else {
				s.innerHTML = "<font color='green'>供销商名称符合规则</font>";
				change_submit("true");
			}
		}
	}
	xmlhttp.send();
	if (str.length == 0 || str.indexOf(" ") >= 0) {
		s.innerHTML = "<font color='red'>供销商名称不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>供销商名称符合规则</font>";
		return true;
	}
}
function CheckAdd(obj) {
	var str = obj.value;
	var s = document.getElementById("provider_add");
	// var regex1 = /^\S{0}$/;
	// if(!regex1.test(str)){
	// s.innerHTML = "<font color='red'>地址不能为空字符串</font>";
	// return false;
	// }else {
	// s.innerHTML = "<font color='green'>地址符合规则</font>";
	// return true;
	// }
	if (str.length == 0 || str.charAt(str.length - 1) == " ") {
		s.innerHTML = "<font color='red'>地址不能为空或者含有空格</font>";
		return false;
	} else {
		s.innerHTML = "<font color='green'>地址符合规则</font>";
		return true;
	}

}
function CheckMail2(obj) {
	var str = obj.value;
	// var regex2 = /^[\u4e00-\u9fa5]{0,}$/;
	var regex1 = /^[0-9|(A-Z|a-z)]{5,10}@[(a-z|A-Z)(0-9)]{2,3}\.com(\.cn)?$/;
	var s = document.getElementById("mail");
	if (regex1.test(str)) {
		s.innerHTML = "<font color='green'>邮箱符合规则</font>";
		return true;
	} else {
		s.innerHTML = "<font color='red'>邮箱格式不正确</font>";
		return false;
	}
}
function CheckAccount(obj) {
	var str = obj.value;
	// var regex2 = /^[\u4e00-\u9fa5]{0,}$/;
	var regex1 = /^[a-zA-Z]{1}[0-9]{3,5}$/;
	var s = document.getElementById("account");
	if (regex1.test(str)) {
		s.innerHTML = "<font color='green'>Account符合规则</font>";
		return true;
	} else {
		s.innerHTML = "<font color='red'>Account格式不正确</font>";
		return false;
	}
}
function CheckTelphone(obj) {
	var str = obj.value;
	// var regex2 = /^[\u4e00-\u9fa5]{0,}$/;
	var regex1 = /^[0-9]{1,}$/;
	var s = document.getElementById("provider_tel");
	if (regex1.test(str)) {
		s.innerHTML = "<font color='green'>联系电话符合规则</font>";
		return true;
	} else {
		s.innerHTML = "<font color='red'>联系电话格式不正确</font>";
		return false;
	}
}
function addProviderReg() {
	var s1 = document.getElementById("name");
	var s2 = document.getElementById("tel");
	var s3 = document.getElementById("acc");
	var s4 = document.getElementById("em");
	var s5 = document.getElementById("add");
	if (CheckProviderName(s1) & CheckTelphone(s2) & CheckAccount(s3)
			& CheckMail2(s4) & CheckAdd(s5)) {
		return true;
	} else {

		return false;
	}
}
function addCateReg() {
	var s1 = document.getElementById("c_name");
	var s2 = document.getElementById("c_desc");
	if (checkCateName(s1) & checkCateDesc(s2)) {
		return true;
	} else {

		return false;
	}
}
function addProductReg() {
	var s1 = document.getElementById("p_name");
	var s2 = document.getElementById("p_income_price");
	var s3 = document.getElementById("p_quantity");
	var s4 = document.getElementById("p_sales_price");
	//alert(checkProductName(s1));
	if (checkProductName(s1) & checkProductIncome_price(s2)
			& checkProductQuantity(s3) & checkProductSales_price(s4)) {
		//alert(t+"t");
		if(t > 0){
			return true;
		}
		else{
			return false;
		}
	}
	return false;

}
function editProductReg() {
	var s1 = document.getElementById("p_name");
	var s2 = document.getElementById("p_income_price");
	var s3 = document.getElementById("p_quantity");
	var s4 = document.getElementById("p_sales_price");
	var s5 = document.getElementById("productID");
	// alert(s1.value+s5.value)
	if (checkUpProductName(s1, s5) & checkProductIncome_price(s2)
			& checkProductQuantity(s3) & checkProductSales_price(s4)) {
		if (i > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
function editProviderReg() {
	var s1 = document.getElementById("name");
	var s2 = document.getElementById("tel");
	var s3 = document.getElementById("acc");
	var s4 = document.getElementById("em");
	var s5 = document.getElementById("add");
	var s6 = document.getElementById("providerID");
	// alert()
	if (CheckUpProviderName(s1, s6) & CheckTelphone(s2) & CheckAccount(s3)
			& CheckMail2(s4) & CheckAdd(s5)) {
		if (i > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
function editCategoryReg() {
	alert(i);
	var s5 = document.getElementById("categoryID");
	var s1 = document.getElementById("c_name");
	var s2 = document.getElementById("c_desc");
	if (checkUpCateName(s1, s5) & checkCateDesc(s2)) {
		if (i > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function change_submit(zt) {
	if (zt == "false") {
		i = 1;
	} else {
		i = 0;
	}
}
function checkAddProductName(w){
	if (w == "false") {
		t = 0;
		//alert(t);
	} else {
		t = 1;
		//alert(t+"---");
	}
}
